﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using System.Data;
using System.Linq.Expressions;

public partial class administrator_usercontrol_billsmanager : System.Web.UI.UserControl
{
    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;

            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            setDLTrangThai();
            ListItem l = new ListItem("tất cả", "", true);
            DropDownList8.Items.Add(l);
            setDLNgay();
            setDLThang();
            setDLNam();
            DropDownList5.SelectedValue = DateTime.Now.Day.ToString();
            DropDownList6.SelectedValue = DateTime.Now.Month.ToString(); ;
            DropDownList7.SelectedValue = DateTime.Now.Year.ToString(); ;
            Lbthongbao0.Text = null;
            if (Request.QueryString["cm"] == null)
            {
                List<DatHangBUS> dathangs = new List<DatHangBUS>();
                dathangs = DatHangBUS.GetAll();
                Session["QLDonHang"] = dathangs;
                GridView1.AutoGenerateColumns = false;
                GridView1.DataSource = dathangs;
                GridView1.DataBind();
            }
            else
            {
                string cm =Request.QueryString["cm"];
                if (cm == "edit" && Request.QueryString["ID"] != null)
                {
                    mt1.ActiveViewIndex = 1;
                    int ID = Convert.ToInt32(Request.QueryString["ID"]);
                    DatHangBUS dh = DatHangBUS.GetByID(ID);
                    LbID.Text = dh.ID.ToString();
                    TextBox1.Text = dh.TenKhacHang;
                    TextBox2.Text = dh.SoDTKH;
                    TextBox3.Text = dh.EmailKH;
                    TextBox4.Text = dh.ThoiGianDatHang.ToString();
                    TextBox5.Text = dh.strThanhTien;
                    
                    DropDownList1.SelectedValue = dh.TrangThai.ToString();

                    GridView2.AutoGenerateColumns = false;
                    GridView2.DataSource = dh.ChiTietDatHangs;
                    GridView2.DataBind();
                }
            }
        }
    }

    private void setDLTrangThai()
    {
        ListItem l = new ListItem("Chưa liên lạc", "1", true);
        DropDownList1.Items.Add(l);
        DropDownList8.Items.Add(l);
        l = new ListItem("Chưa giao", "2", true);
        DropDownList1.Items.Add(l);
        DropDownList8.Items.Add(l);
        l = new ListItem("Đã giao", "3", true);
        DropDownList1.Items.Add(l);
        DropDownList8.Items.Add(l);
    }

    private void setDLNgay()
    {
        for (int i = 1; i < 32; i++)
        {
            ListItem l = new ListItem(i.ToString(), i.ToString(), true);
            DropDownList2.Items.Add(l);
            DropDownList5.Items.Add(l);
        }
    }
    private void setDLThang()
    {
        for (int i = 1; i <= 12; i++)
        {
            ListItem l = new ListItem(i.ToString(), i.ToString(), true);
            DropDownList3.Items.Add(l);
            DropDownList6.Items.Add(l);
        }
    }
    private void setDLNam()
    {
        for (int i = 1990; i <= 2020; i++)
        {
            ListItem l = new ListItem(i.ToString(), i.ToString(), true);
            DropDownList4.Items.Add(l);
            DropDownList7.Items.Add(l);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("donhang.aspx");
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            Response.Redirect("donhang.aspx?cm=edit&ID=" + e.CommandArgument.ToString());
        }
        if (e.CommandName == "Delete")
        {
            int ID = Convert.ToInt32(e.CommandArgument);
            DatHangBUS dh = DatHangBUS.GetByID(ID);
            try
            {
                dh.Xoa();
                Response.Redirect("donhang.aspx");
            }
            catch
            {
                Lbthongbao0.Text = "Không Thể Xóa Dữ Liệu !!!";
            }

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int ID = Convert.ToInt32(LbID.Text);
        DatHangBUS dh = DatHangBUS.GetByID(ID);
        dh.TrangThai = Convert.ToInt32(DropDownList1.SelectedValue);
        dh.UpdateTrangThai();
        Response.Redirect("donhang.aspx");
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lkbXoa");

            l.Attributes.Add("onclick", "javascript:return " +
                 "confirm('bạn có muốn xóa đơn hàng " +
                 DataBinder.Eval(e.Row.DataItem, "strThoiGianDatHang") + " ?')");

        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        List<DatHangBUS> dathangs = new List<DatHangBUS>();
        dathangs = (List<DatHangBUS>)Session["QLDonHang"];
        GridView1.DataSource = dathangs;
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        List<DatHangBUS> myGridResults = (List<DatHangBUS>)Session["QLDonHang"];


        if (myGridResults != null)
        {
            List<DatHangBUS> viewstate = new List<DatHangBUS>();
            var param = Expression.Parameter(typeof(DatHangBUS), e.SortExpression);
            var sortExpression = Expression.Lambda<Func<DatHangBUS, object>>(Expression.Convert(Expression.Property(param, e.SortExpression), typeof(object)), param);


            if (GridViewSortDirection == SortDirection.Ascending)
            {
                viewstate = myGridResults.AsQueryable<DatHangBUS>().OrderBy(sortExpression).ToList<DatHangBUS>();
                GridView1.DataSource = myGridResults.AsQueryable<DatHangBUS>().OrderBy(sortExpression).ToList<DatHangBUS>();
                GridViewSortDirection = SortDirection.Descending;
            }
            else
            {
                viewstate = myGridResults.AsQueryable<DatHangBUS>().OrderByDescending(sortExpression).ToList<DatHangBUS>();
                GridView1.DataSource = myGridResults.AsQueryable<DatHangBUS>().OrderByDescending(sortExpression).ToList<DatHangBUS>();
                GridViewSortDirection = SortDirection.Ascending;
            };


            GridView1.DataBind();
            Session["QLDonHang"] = viewstate;
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        DateTime tungay = new DateTime();
        DateTime denngay = new DateTime();
        try { tungay=new DateTime(Convert.ToInt16(DropDownList4.SelectedValue),Convert.ToInt16(DropDownList3.SelectedValue),Convert.ToInt16(DropDownList2.SelectedValue),0,0,0);}
        catch
        {
            tungay = new DateTime(Convert.ToInt16(DropDownList4.SelectedValue),Convert.ToInt16(DropDownList3.SelectedValue),1,0,0,0);
            tungay=tungay.AddMonths(1);
        }
        try { denngay = new DateTime(Convert.ToInt16(DropDownList7.SelectedValue), Convert.ToInt16(DropDownList6.SelectedValue), Convert.ToInt16(DropDownList5.SelectedValue),23,59,59); }
        catch
        {
            denngay = new DateTime(Convert.ToInt16(DropDownList4.SelectedValue),Convert.ToInt16(DropDownList3.SelectedValue),1,23,59,59);
            denngay=denngay.AddMonths(1);
            
        }
        string tt = DropDownList8.SelectedValue;
        List<DatHangBUS> dathangs = new List<DatHangBUS>();
        dathangs = DatHangBUS.GetAll();
        List<DatHangBUS> resuilt = new List<DatHangBUS>();
        resuilt =    dathangs.Where(p=>(p.ThoiGianDatHang.CompareTo(tungay)>=0&&p.ThoiGianDatHang.CompareTo(denngay)<=0&&p.TrangThai.ToString().Contains(tt))).ToList();
        Session["QLDonHang"] = resuilt;
        GridView1.DataSource=resuilt;
        GridView1.DataBind();
        if (resuilt.Count == 0)
        {
            Lbthongbao0.Text = "Không có dữ liệu";
        }
        else
        {
            Lbthongbao0.Text = "";
        }
        
    }
}
